package stdio.costa.imoca.servlet;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import stdio.costa.imoca.dto.ActivityListDTO;
import stdio.costa.imoca.dto.ActivityWithPostcardbackDTO;
import stdio.costa.imoca.model.Activity;
import stdio.costa.imoca.utils.TimeUtil;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;

@Controller
@RequestMapping(value = "/activity")
public class ActivityController extends BaseController {

	@RequestMapping(value = "create_activity")
	public String toCreateActivityPage(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result) {
		return "create_activity";
	}

	@RequestMapping(value = "/add_image", method = RequestMethod.GET)
	public String toAddImageToActivity(Model model) {
		return "add_image_to_activity";
	}

	@RequestMapping(value = "/create")
	public String add(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {

		String accessToken = (String) request.getSession(false).getAttribute(
				"accessToken");

		String name = request.getParameter("name");
		String description = request.getParameter("description");

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);
		param.add("name", name);
		param.add("description", description);
		param.add("startTime", TimeUtil.getDateString());
		param.add("endTime", TimeUtil.getDateString());

		Activity activity = service.path("activity").path("create")
				.queryParams(param).type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).post(Activity.class);

		System.out.println(activity);

		param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);

		ActivityListDTO activityList = service.path("activity")
				.path("get_list").queryParams(param)
				.type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON)
				.get(ActivityListDTO.class);

		List<Activity> activities = activityList.getActivities();
		result.put("activities", activities);

		return "activities";
	}

	@RequestMapping(value = "/get_postcardbacks")
	public String getPostcardBacks(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {

		String accessToken = (String) request.getSession(false).getAttribute(
				"accessToken");

		String uuid = request.getParameter("uuid");

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);
		param.add("activity_uuid", uuid);

		ActivityWithPostcardbackDTO dto = service.path("activity")
				.path("get_postcardbacks").queryParams(param)
				.type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON)
				.get(ActivityWithPostcardbackDTO.class);
		
		System.out.println(dto);
		
		result.put("activity", dto);
		
		return "activity_with_postcardbacks";
	}

	
	@RequestMapping(value = "/get_detail")
	public String getDetailWithoutPostcardbacks(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {

		String accessToken = (String) request.getSession(false).getAttribute(
				"accessToken");

		String uuid = request.getParameter("uuid");

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);

		Activity activity = service.path("activity").path("get").path(uuid)
				.queryParams(param).type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).get(Activity.class);
		
		

		return null;
	}
	
	
	@RequestMapping(value = "/get_activities")
	public String getActivities(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {

		String accessToken = (String) request.getSession(false).getAttribute(
				"accessToken");

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);

		ActivityListDTO activityList = service.path("activity")
				.path("get_list").queryParams(param)
				.type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON)
				.get(ActivityListDTO.class);

		List<Activity> activities = activityList.getActivities();
		result.put("activities", activities);

		return "activities";
	}
}
